package com.monkeygroup.mygoplus.ecchat.ui.contact;

import android.content.Intent;
import android.graphics.Bitmap;
import android.media.Image;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.monkeygroup.mygoplus.MyGoApplication;
import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/30.
 */
public class MyDRCodeActivity extends BaseActivity implements View.OnClickListener {
    private TextView my_nickname, my_userid;
    private ImageView my_drcode_image;
    private final int QR_WIDTH = 400, QR_HEIGHT = 400;
    private final String TAG = "MyDRCodeActivity";

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_my_drcode);
    }

    @Override
    public void initView() {
        my_nickname = (TextView) findViewById(R.id.my_nickname);
        if (MyGoApplication.myNickname.equals("")) {
            my_nickname.setText(MyGoApplication.myECUserid);
        } else {
            my_nickname.setText(MyGoApplication.myNickname);
        }

        my_userid = (TextView) findViewById(R.id.my_userid);
        my_userid.setText(MyGoApplication.myECUserid);
        my_drcode_image = (ImageView) findViewById(R.id.my_drcode_image);
    }

    @Override
    public void setListener() {
        findViewById(R.id.layout_back).setOnClickListener(this);
    }

    @Override
    public void doEvent() {
        String myCode = mapToJson(getCodeObject());

        createQRImage(myCode);
    }

    private Map<String, Object> getCodeObject() {
        Map<String, Object> map = new HashMap<>();
        Map<String, String> data = new HashMap<>();
        data.put("codeType", "id");
        data.put("codeContent", MyGoApplication.myECUserid);

        map.put("mygoqrcode", data);

        return map;
    }

    /**
     * 將moible生成二維碼
     **/
    private void createQRImage(String content) {
        try {
            if (content == null || "".equals(content) || content.length() < 1) {
                return;
            }

            Hashtable<EncodeHintType, String> hints = new Hashtable<>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            //图像数据转换，使用了矩阵转换
            BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
            int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
            //下面这里按照二维码的算法，逐个生成二维码的图片，
            //两个for循环是图片横列扫描的结果
            for (int y = 0; y < QR_HEIGHT; y++) {
                for (int x = 0; x < QR_WIDTH; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * QR_WIDTH + x] = 0xff000000;
                    } else {
                        pixels[y * QR_WIDTH + x] = 0xffffffff;
                    }
                }
            }
            //生成二维码图片的格式，使用ARGB_8888
            Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
            //显示到一个ImageView上面
            my_drcode_image.setImageBitmap(bitmap);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * map转JSON
     **/
    public static <T> String mapToJson(Map<String, T> map) {
        Gson gson = new Gson();
        String jsonStr = gson.toJson(map);

        return jsonStr;
    }
}
